package models;
import java.util.*;

import javax.persistence.*;

import org.hibernate.annotations.Type;

import play.data.validation.Required;
import play.db.jpa.*;
 
@Entity
public class Coordonnee extends Model
{
	@Required
	public int position_x;
	
	@Required
	public int position_y;
	
	@Required
	@ManyToOne
    public Carte carte;
	
	@Required
	@Type(type = "org.hibernate.type.NumericBooleanType")
	public boolean est_eau;
	
	@ManyToOne
    public Condition_meteo condition;

	public Coordonnee(int position_x, int position_y, Carte carte,
			Condition_meteo condition, boolean est_eau ) 
	{
		this.position_x = position_x;
		this.position_y = position_y;
		this.carte = carte;
		this.condition = condition;
		this.est_eau = est_eau;
	}
	
	public Coordonnee() {
	}

	public String toString()
	{
		return this.carte.libelle_carte + " > " + Integer.toString(position_x) + ";" + Integer.toString(position_y);
	}
	
	// Retourne le dernier id
	public static long getLastId()
	{
		Coordonnee last_coord = Coordonnee.find("order by id desc").first();
		return last_coord.id;
	}
	
	// Retourne l'id de la coordonnée, ou 0 si rien n'a été trouvé
	public static long getIdByCarteAndCoords(long carte_id, int x, int y)
	{
		Coordonnee coord = Coordonnee.find("byCarte_idAndPosition_xAndPosition_y", carte_id, x, y).first();
		if(coord != null)
		{
			return coord.id;
		}
		else
		{
			return 0;
		}
	}
	
	
}
